package com.itheima.reggie.service.impl;

import cn.hutool.core.util.HashUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.reggie.common.CustomException;
import com.itheima.reggie.common.UserHolder;
import com.itheima.reggie.domain.Address;
import com.itheima.reggie.mapper.AddressMapper;
import com.itheima.reggie.service.AddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


// 收货地址
@Service
@Transactional
public class AddressServiceImpl implements AddressService {

    @Autowired
    private AddressMapper addressMapper;

    @Autowired
    private RedisTemplate redisTemplate;

    // 查询收货地址列表
    @Override
    public List<Address> listAddress() {

        // 1.构建条件  select * from address_book where user_id = ?
        LambdaQueryWrapper<Address> lambdaQw = new LambdaQueryWrapper<>();
        lambdaQw.eq(Address::getUserId, UserHolder.get().getId());

        return addressMapper.selectList(lambdaQw);
    }

    // 新增收货地址
    @Override
    public void add(Address address) {
        // 1.补全信息
        address.setUserId(UserHolder.get().getId()); // 用户id
        address.setIsDefault(0); // 是否默认地址

        // 2.保存数据库
        addressMapper.insert(address);
    }

    // 设置默认地址
    @Override          // 这个是基础：newAddress形参名随便起，相同只是为了好记忆
    public void setDefault(Address newAddress) {

        // 1.先把该用户所有地址设置为非默认   update address_book set is_default = 0 where user_id = ?

        // 1-1 设置参数 ,把地址表的IsDefault属性初始化为0
        Address oldAddress = new Address();
        oldAddress.setIsDefault(0); // 非默认        is_default = 0

        // 1-2 构建条件
        LambdaQueryWrapper<Address> qw = new LambdaQueryWrapper<>();
        qw.eq(Address::getUserId,UserHolder.get().getId());     // where user_id = ?

        // 1-3 执行更新 ，将SQL合并执行，将该用户的所有地址的默认情况都改成0
        addressMapper.update(oldAddress,qw);  // update address_book set

        // 2.把新传递地址设置为默认
        newAddress.setIsDefault(1); // 默认
        addressMapper.updateById(newAddress);

    }

    // 查询默认地址
    @Override
    public Address findDefault() {

        // 1.构建条件 select * from address_book where user_id = 1633299848511954946 and is_default = 1
        LambdaQueryWrapper<Address> qw = new LambdaQueryWrapper<>();
        qw.eq(Address::getUserId,UserHolder.get().getId());  //where user_id = 1633299848511954946
        qw.eq(Address::getIsDefault,1);  //is_default = 1

        // 2.执行查询
        return addressMapper.selectOne(qw);  //select * from address_book
    }

    //回显地址 根据ID查
    @Override
    public Address addId(Long id) {

        // 根据ID查询
        LambdaQueryWrapper<Address> qw = new LambdaQueryWrapper<>();
        qw.eq(Address::getUserId,id);  //  UserId = #{id}

        return addressMapper.selectById(id);
    }

    //修改地址  update
    @Override
    public void updateAdd(Address address) {

       addressMapper.updateById(address);

    }


}
